home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / IBTip / Source / Fdset.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.3 KB  |  70 lines

  1. #import "Fdset.h"
  2. #import <libc.h>
  3.  
  4. @implementation Fdset
  5.   
  6. + new
  7. {
  8.   self = [super new];
  9.   FD_ZERO(&fds);
  10.   FD_ZERO(&ready_fds);
  11.   nfds = 0;
  12.   return self;
  13. }
  14.  
  15. - addfd:(int)thisfd
  16. {
  17.   FD_SET(thisfd,&fds);
  18.   if (thisfd+1 > nfds)
  19.     nfds = thisfd+1;
  20.   return self;
  21. }
  22.  
  23. - (BOOL) isfdReady:(int)thisfd // returns YES if given descriptor is ready
  24. {
  25.   return (FD_ISSET(thisfd,&ready_fds));
  26. }
  27.  
  28. - (int) waitOnInputForever  // returns number of file descriptors which are ready
  29. {
  30.   int nfound;
  31.   ready_fds = fds;
  32.   nfound = select(nfds, &ready_fds, 0,0,0);
  33.   return (nfound);
  34. }
  35.  
  36. - (int) waitOnInputFor:(int)seconds  //waits no more than number of seconds
  37. {
  38.   int nfound;
  39.   struct timeval timeout;
  40.   timeout.tv_usec = 0;
  41.   timeout.tv_sec = seconds;
  42.   ready_fds = fds;
  43.   nfound = select(nfds, &ready_fds, 0,0, &timeout);
  44.   return (nfound);
  45. }
  46.  
  47. - (int) waitOnInputFor:(int)seconds :(int)microseconds  // for fractional timeouts
  48. {
  49.   int nfound;
  50.   struct timeval timeout;
  51.   timeout.tv_usec = microseconds;
  52.   timeout.tv_sec = seconds;
  53.   ready_fds = fds;
  54.   nfound = select(nfds, &ready_fds, 0,0, &timeout);
  55.   return (nfound);
  56. }
  57.  
  58. - (int) getReadyfd  //returns one of the ready file descriptors, marks it unready
  59. {
  60.   int fd;
  61.   for (fd=0; fd<nfds; fd++)
  62.     if (FD_ISSET(fd,&ready_fds)) {
  63.       FD_CLR(fd,&ready_fds);
  64.       return fd;
  65.     }
  66.   return (-1);
  67. }
  68.  
  69. @end
  70.